package com.ucode.wap.cgms.controller;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.ucode.cgms.service.CgmsCompanionCategoryService;
import com.ucode.cgms.service.CgmsCompanionProductService;
import com.ucode.cgms.service.CgmsCompanionUserService;
import com.ucode.cgms.service.CgmsSpecialZoneService;
import com.ucode.model.cgms.CgmsCompanionCategory;
import com.ucode.model.cgms.CgmsCompanionProduct;
import com.ucode.model.cgms.CgmsCompanionUser;
import com.ucode.model.cgms.CgmsSpecialZone;
import com.ucode.model.cgms.convert.CgmsCompanionProductConvert;
import com.ucode.model.cgms.vo.CgmsCompanionProductVO;
import com.ucode.tool.annotation.NeedLogin;
import com.ucode.tool.base.Paginator;
import com.ucode.tool.base.ResponseResult;
import com.ucode.tool.base.enums.CommonReleaseStatusEnum;
import com.ucode.wap.base.core.CurrentLoginUserHandler;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "移动端 - 陪玩社区系统 - 陪玩商品")
@RestController
@RequestMapping("/cgms/companion-product")
@Validated
public class ApiCgmsCompanionProductController {

    private static Log log = LogFactory.get();
    @Resource
    private CgmsCompanionCategoryService cgmsCompanionCategoryService;
    @Resource
    private CgmsCompanionProductService cgmsCompanionProductService;
    @Resource
    private CgmsSpecialZoneService cgmsSpecialZoneService;
    @Resource
    private CgmsCompanionUserService cgmsCompanionUserService;
    @Resource
    private CurrentLoginUserHandler currentLoginUserHandler;

    @GetMapping(value = "/home/hot-list")
    @Operation(summary = "首页热门服务商品列表")
    public ResponseResult<Paginator<CgmsCompanionProduct>> hotListByHome(
            @Schema(description = "页码", example = "1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @Schema(description = "每页记录数", example = "4") @RequestParam(value = "pageSize", defaultValue = "4") Integer pageSize) {

        Map<String,Object> condition = new HashMap<>();
        condition.put("recHot",true);
        condition.put("status", CommonReleaseStatusEnum.RELEASE.getCode());
        condition.put("deltag",false);

        Paginator<CgmsCompanionProduct> paginator = cgmsCompanionProductService.queryPageForList(condition,pageSize,pageNum);
        return ResponseResult.success(paginator);
    }

    @GetMapping(value = "/home/benefit-list")
    @Operation(summary = "首页优惠服务商品列表")
    public ResponseResult<Paginator<CgmsCompanionProduct>> benefitListByHome(
            @Schema(description = "页码", example = "1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @Schema(description = "每页记录数", example = "4") @RequestParam(value = "pageSize", defaultValue = "4") Integer pageSize) {

        Map<String,Object> condition = new HashMap<>();
        condition.put("recBenefit",true);
        condition.put("status", CommonReleaseStatusEnum.RELEASE.getCode());
        condition.put("deltag",false);

        Paginator<CgmsCompanionProduct> paginator = cgmsCompanionProductService.queryPageForList(condition,pageSize,pageNum);
        return ResponseResult.success(paginator);
    }

    @GetMapping(value = "/home/official-self-list")
    @Operation(summary = "首页官方自营服务商品列表")
    public ResponseResult<Paginator<CgmsCompanionProduct>> officialSelfListByHome(
            @Schema(description = "页码", example = "1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @Schema(description = "每页记录数", example = "4") @RequestParam(value = "pageSize", defaultValue = "4") Integer pageSize) {

        Map<String,Object> condition = new HashMap<>();
        condition.put("providerId",0L);
        condition.put("status", CommonReleaseStatusEnum.RELEASE.getCode());
        condition.put("deltag",false);

        Paginator<CgmsCompanionProduct> paginator = cgmsCompanionProductService.queryPageForList(condition,pageSize,pageNum);
        return ResponseResult.success(paginator);
    }

    @Operation(summary = "分页列表")
    @GetMapping(value = "/page-list")
    public ResponseResult<Paginator<CgmsCompanionProduct>> pageList(
            @Schema(description = "关键字") @RequestParam(value = "keywords",required=false) String keywords,
            @Schema(description = "专区编号") @RequestParam(value = "zoneId",required = false) Long zoneId,
                @Schema(description = "专区分类编号") @RequestParam(value = "categoryId",required = false) Long categoryId,
            @Schema(description = "提供者编号") @RequestParam(value = "providerId",required = false) Long providerId,
            @Schema(description = "页码", example = "1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @Schema(description = "每页记录数", example = "5") @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {

        Map<String,Object> condition = new HashMap<>();
        if(StrUtil.isNotBlank(keywords))
            condition.put("keywords", StrUtil.concat(true,"%", keywords,"%"));
        condition.put("zoneId",zoneId);
        condition.put("zoneId",zoneId);
        condition.put("categoryId",categoryId);
        condition.put("providerId",0L);
        condition.put("status", CommonReleaseStatusEnum.RELEASE.getCode());
        condition.put("deltag",false);

        Paginator<CgmsCompanionProduct> paginator = cgmsCompanionProductService.queryPageForList(condition,pageSize,pageNum);
        return ResponseResult.success(paginator);
    }

    @GetMapping(value = "/details/{id}")
    @Parameter(description="主键ID",name = "id", required = true,in = ParameterIn.PATH,example = "1024")
    @Operation(summary = "服务商品信息详情")
    public ResponseResult<CgmsCompanionProductVO> getDetails(@PathVariable(name="id",required=true) Long id) {
        CgmsCompanionProduct bean = cgmsCompanionProductService.validateRelease(id);
        CgmsCompanionProductVO vo = CgmsCompanionProductConvert.INSTANCE.convert(bean);
        if(ObjUtil.isNotNull(bean.getZoneId())){
            CgmsSpecialZone zone = cgmsSpecialZoneService.selectByPrimaryKey(bean.getZoneId());
            if(ObjUtil.isNotNull(zone)) {
                vo.setZoneName(zone.getName());
            }
            CgmsCompanionCategory category = cgmsCompanionCategoryService.selectByPrimaryKey(bean.getCategoryId());
            if(ObjUtil.isNotNull(category)) {
                vo.setCategoryName(category.getName());
            }

            if(ObjUtil.isNotNull(vo.getProviderId()) && !ObjUtil.equals(vo.getProviderId(), 0L)){
                CgmsCompanionUser provider = cgmsCompanionUserService.selectByPrimaryKey(vo.getProviderId());
                if(ObjUtil.isNotNull(provider)) {
                    vo.setNickname(provider.getNickName());
                    vo.setAvatar(provider.getAvatar());
                }
            }
        }
        return ResponseResult.success(vo);
    }

    @NeedLogin(validate = true)
    @GetMapping(value = "/current-user/list")
    @Operation(summary = "当前用户发布的陪玩服务商品")
    public ResponseResult<List<CgmsCompanionProduct>> currentUserCompanionProductList() {
        Long userId = currentLoginUserHandler.getCurrentUserId();
        Map<String,Object> condition = new HashMap<>();
        condition.put("providerId",userId);
        condition.put("deltag",false);

        List<CgmsCompanionProduct> list = cgmsCompanionProductService.queryObjectForList(condition,0);
        return ResponseResult.success(list);
    }

    @GetMapping(value = "/list-by-user/{userId}")
    @Parameter(description="陪玩ID",name = "id", required = true,in = ParameterIn.PATH,example = "1024")
    @Operation(summary = "陪玩用户已发布的服务商品列表")
    public ResponseResult<List<CgmsCompanionProduct>> getCompanionProductListByProviderId(@PathVariable(name="userId",required=true) Long userId) {
        Map<String,Object> condition = new HashMap<>();
        condition.put("providerId",userId);
        condition.put("status", CommonReleaseStatusEnum.RELEASE.getCode());
        condition.put("deltag",false);

        List<CgmsCompanionProduct> list = cgmsCompanionProductService.queryObjectForList(condition,0);
        return ResponseResult.success(list);
    }


}
